/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.saen.nextincloud.dao;

import co.saen.nextincloud.hibernate.HibernateUtil;
import co.saen.nextincloud.model.Users;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Felipe Rolvar
 */
public class UsersDAO {

    public UsersDAO() {
    }

    public String createUser(Users user) {
        String msg;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(user);
            session.beginTransaction().commit();
            msg = "ok";
        } catch (Exception e) {
            msg = "Error al Insertar Nuevo Usuario " + e.getMessage();
            session.beginTransaction().rollback();
        } finally {
            session.getSessionFactory().close();
        }
        return msg;
    }

    public String updateUser(Users user) {
        String msg;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(user);
            session.beginTransaction().commit();
            msg = "ok";
        } catch (Exception e) {
            msg = "Error al Actualizar Usuario " + e.getMessage();
            session.beginTransaction().rollback();
        } finally {
            session.getSessionFactory().close();
        }
        return msg;
    }

    public String deleteUser(Users user) {
        String msg;
        user.setUsrActv(Boolean.FALSE);
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.merge(user);
            session.beginTransaction().commit();
            msg = "ok";
        } catch (Exception e) {
            msg = "Error al Eliminar Usuario " + e.getMessage();
            session.beginTransaction().rollback();
        } finally {
            session.getSessionFactory().close();
        }
        return msg;
    }

    public List<Users> allUsers(Boolean includeInactive) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (includeInactive == true) {
            return session.createQuery("from Users WHERE usrActv = '0'").list();
        } else {
            return session.createQuery("from Users WHERE usrActv = '1'").list();
        }
    }

    public Users searchByCode(String usrCode) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Users) session.load(Users.class, usrCode);
    }

    public Users searchByUser(Users user) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String sql = "SELECT u FROM Users u WHERE usrCode=:user AND usrPswd=:pass";
        Query query = session.createQuery(sql);
        query.setParameter("user", user.getUsrCode());
        query.setParameter("pass", user.getUsrPswd());
        return (Users) query.uniqueResult();
    }
}
